package bdz.projects.lavender.doi.model
{
	import bdz.common.events.UploadManagerEvent;
	import bdz.common.events.XMLLoaderEvent;
	import bdz.common.managers.UploadManager;
	import bdz.common.model.XMLLoader;
	import bdz.common.model.AppEnvironment;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLVariables;

	public class DOIConnector extends EventDispatcher
	{
		private static var instance:DOIConnector;
		private var _configuration:Object;
		public var umanager:UploadManager;
		private var currentuploader:Function;
		
		private var GET_ALL:String = "serviceSelect.asmx/GetCategoryHierarchy_CategoryID_or_ALL";
		//private var GET_ALL:String = "static.xml";
		private var UPLOAD_FILE:String = "serviceUploadAssets.asmx/UploadAssets";
		private var GET_ALL_CATEGORIES:String = "serviceSelect.asmx/GetAllCategories";
		private var GET_MODULE_CONTENTS:String = "serviceSelect.asmx/GetModuleHierarchy_ModuleID";
		
		private var ADD_MODULE:String = "serviceInsert.asmx/Insert_Module";
		private var ADD_CATEGORY:String = "serviceInsert.asmx/Insert_Category";
		private var ADD_STEP:String = "serviceInsert.asmx/Insert_Step";
		
		private var UPDATE_MODULE:String = "serviceUpdate.asmx/Update_Module";
		private var UPDATE_CATEGORY:String = "serviceUpdate.asmx/Update_Category";
		private var UPDATE_STEP:String = "serviceUpdate.asmx/Update_Step";
		
		private var DELETE_MODULE:String = "serviceDelete.asmx/DeleteModule";
		private var DELETE_CATEGORY:String = "serviceDelete.asmx/DeleteCategory";
		private var DELETE_STEP:String = "serviceDelete.asmx/DeleteStep";
		
		private var UPDATE_ORDER:String = "serviceInsert.asmx/SetBatchSorting";
		
		private var GET_GLOBAL:String = "serviceSelect.asmx/GetGlobalValue";
		//private var GET_GLOBAL:String = "global.xml";

		private var UPDATE_GLOBAL:String = "serviceUpdate.asmx/Update_GlobalValue";
		
		public function DOIConnector(se:SingletonEnforcer)
		{
			super();
		}
		public static function getInstance(config:Object):DOIConnector{
			if(!instance){
				instance = new DOIConnector(new SingletonEnforcer());
				instance.initialize(config);
			}
			return instance;
		}
		/** UPLOAD CODE
		 * 
		 */
		private function dispatchUploadComplete(evt:UploadManagerEvent):void{
			currentuploader(evt);
			dispatchEvent(evt);
		}
		private function dispatchUploadError(evt:IOErrorEvent):void{
			dispatchEvent(evt);
		}
		private function dispatchUploadStart(evt:UploadManagerEvent):void{
			dispatchEvent(evt);
			
		}
		private function createUploadManager(onselect:Function, uvars:Object = null):void{
			umanager = new UploadManager();
			umanager.addEventListener(UploadManagerEvent.UPLOAD_COMPLETE, dispatchUploadComplete);
			umanager.addEventListener(Event.SELECT, onselect);
			umanager.addEventListener(IOErrorEvent.IO_ERROR, dispatchUploadError);
			umanager.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchUploadError);
			umanager.addEventListener(UploadManagerEvent.UPLOAD_START, dispatchUploadStart);
			if(uvars){
				for(var prop:String in uvars){
					umanager.addVariables(prop, uvars[prop]);
				}
			}
			umanager.url = _configuration.baseURL+UPLOAD_FILE;
		}
		public function browseForSWF(onselect:Function, oncomplete:Function, uvars:Object = null):void{
			currentuploader = oncomplete;
			createUploadManager(onselect, uvars);
			umanager.browseForCustomType("SWF File", "*.swf");
		}
		public function browseForVideo(onselect:Function, oncomplete:Function, uvars:Object = null):void{
			currentuploader = oncomplete;
			createUploadManager(onselect, uvars);
			umanager.browseForCustomType("Flash Video File", "*.flv");
		}
		public function browseForImage(onselect:Function, oncomplete:Function, uvars:Object = null):void{
			currentuploader = oncomplete;
			createUploadManager(onselect, uvars);
			umanager.browseForImage();
		}
		
		public function browseForFile(onselect:Function, oncomplete:Function, uvars:Object = null, audio:Boolean = false, ffilter:Object = null):void{
			currentuploader = oncomplete;
			createUploadManager(onselect, uvars);
			if(audio){
				umanager.browseForCustomType("Audio File", "*.mp3;");
			}else{
				if(ffilter == null){
					umanager.browseForCustomType("Images/SWF File", "*.swf;*.jpg;*.png;*.jpeg;JPEG");
				}else{
					umanager.browseForCustomType(ffilter.label, ffilter.filter);
				}
			}
		}
		public function browseForPDF(onselect:Function, oncomplete:Function, uvars:Object = null):void{
			currentuploader = oncomplete;
			createUploadManager(onselect, uvars);
			umanager.browseForCustomType("PDF File", "*.pdf;");
			
		}
		public function upload(obj:Object = null):void{
			if(obj){
				for(var prop:String in obj){
					umanager.variables[prop] = obj[prop];
				}
			}
			umanager.uploadFile();
		}
		
		/** END UPLOAD CODE
		 * 
		 */
		private function createLoader(result:Function, fault:Function):XMLLoader{
		 	var xm:XMLLoader = new XMLLoader();
			xm.addEventListener(XMLLoaderEvent.RESULT, result);
			if(fault != null){
				xm.addEventListener(XMLLoaderEvent.FAULT, fault);
				xm.addEventListener(XMLLoaderEvent.IOFAULT, fault);
			}
			return xm;
		 }
		private function initialize(cfg:Object):void{
			_configuration = cfg;
		}
		private function onTestCompressedLoaded(evt:XMLLoaderEvent):void{
			trace(evt.data);
		}
		public function testCompressed():void{
			var xm:XMLLoader = new XMLLoader();
			xm.addEventListener(XMLLoaderEvent.RESULT, onTestCompressedLoaded);
			xm.location = _configuration.baseURL+"serviceSelect.asmx/GetCategoryHierarchy_Compress";
			xm.sendCompressed();
		}
		public function getModuleContents(mid:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			xm.location = _configuration.baseURL+GET_MODULE_CONTENTS;
			var uvars:URLVariables = new URLVariables();
			uvars.ModuleID = mid;
			xm.send(uvars);
		}
		public function getCategoryContents(cid:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			if (AppEnvironment.getEnv().getSetting("DEMO_MODE")) {
				xm.location = "static.xml";
			}else{
				xm.location = _configuration.baseURL + GET_ALL;
			}
			var uvars:URLVariables = new URLVariables();
			uvars.CategoryID = cid;
			xm.send(uvars);
		}
		public function getAllCategories(result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			xm.location = _configuration.baseURL+GET_ALL_CATEGORIES;
			var uvars:URLVariables = new URLVariables();
			xm.send(uvars);
		}
		public function getAll(result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			xm.location = _configuration.baseURL + GET_ALL;
			
			var uvars:URLVariables = new URLVariables();
			uvars.CategoryID = "";
			
			xm.send(uvars);
		}
		public function getGlobalValues(result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			xm.location = _configuration.baseURL+GET_GLOBAL;
			xm.send();
		}
		public function updateGlobalValues(gvalue:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.prmValue = gvalue;
			xm.location = _configuration.baseURL+UPDATE_GLOBAL;
			xm.send(uvars);
		}
		public function updateOrder(vals:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.prmXMLData = vals;
			xm.location = _configuration.baseURL+UPDATE_ORDER;
			xm.send(uvars);
		}
		public function insertCategory(catobj:Object, result:Function, order:Number, fault:Function = null):void{
			
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.CategoryName = catobj.catName;
			uvars.ShortName = catobj.shortName;
			uvars.HTMLImage = catobj.htmlimage;
			uvars.SWFImage = catobj.swfimage;
			uvars.Definition = catobj.definition || "";
			uvars.order = order;
			xm.location = _configuration.baseURL+ADD_CATEGORY;
			xm.send(uvars);
			
		}
		public function editCategory(catvars:Object, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			
			var uvars:URLVariables = new URLVariables();
			
			for(var prop:String in catvars){
				uvars[prop] = catvars[prop];
			}
			trace(uvars);
			xm.location = _configuration.baseURL+UPDATE_CATEGORY;
			xm.send(uvars);
		}
		public function deleteCategory(id:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.CategoryID = id;
			xm.location = _configuration.baseURL+DELETE_CATEGORY;
			xm.send(uvars);
		}
		public function getCategories(result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			xm.location = _configuration.baseURL+GET_ALL_CATEGORIES;
			xm.send();
		}
		
		public function insertModule(modvars:Object, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			for(var prop:String in modvars){
				uvars[prop] = modvars[prop];
			}
			xm.location = _configuration.baseURL+ADD_MODULE;
			xm.send(uvars);
			
		}
		public function editModule(modvars:Object, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			for(var prop:String in modvars){
				uvars[prop] = modvars[prop];
			}
			
			xm.location = _configuration.baseURL+UPDATE_MODULE;
			xm.send(uvars);
			
		}
		public function deleteModule(mid:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.ModuleID = mid;
			xm.location = _configuration.baseURL+DELETE_MODULE;
			xm.send(uvars);
		}
		
		public function insertStep(stepvars:Object, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			for(var prop:String in stepvars){
				uvars[prop] = stepvars[prop];
			}
			xm.location = _configuration.baseURL+ADD_STEP;
			xm.send(uvars);
		}
		public function editStep(stepvars:Object, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			for(var prop:String in stepvars){
				uvars[prop] = stepvars[prop];
			}
			xm.location = _configuration.baseURL+UPDATE_STEP;
			xm.send(uvars);
		}
		public function deleteStep(sid:String, result:Function, fault:Function = null):void{
			var xm:XMLLoader = createLoader(result, fault);
			var uvars:URLVariables = new URLVariables();
			uvars.StepID = sid;
			xm.location = _configuration.baseURL+DELETE_STEP;
			xm.send(uvars);
		}
		public static function get configuration():Object{
			return instance._configuration;
		}
		
	}
}
/**
 * @private
 * 
 */
internal class SingletonEnforcer{}